package com.paomi.client.activity.person;

import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.hannesdorfmann.mosby3.mvp.MvpView;
import com.paomi.client.R;
import com.paomi.client.activity.person.View.OpinionView;
import com.paomi.client.activity.person.presenter.OpinionPre;
import com.paomi.client.base.BaseMvpActivity;
import com.paomi.client.bean.BaseJSON;
import com.paomi.client.utils.ToastUtil;
import com.paomi.client.utils.Util;

import butterknife.BindView;
import butterknife.OnClick;

public class OpinionActivity extends BaseMvpActivity<OpinionView, OpinionPre> implements OpinionView {

    @BindView(R.id.content_et)
    EditText content_et;
    @BindView(R.id.tv_et_num)
    TextView tv_et_num;
    @BindView(R.id.tv_opinion)
    TextView tv_opinion;
    @BindView(R.id.button_goback)
    TextView button_goback;

    @NonNull
    @Override
    public OpinionPre createPresenter() {
        return new OpinionPre();
    }


    @Override
    protected String setTitle() {
        return getResources().getString(R.string.feedback_opinion);
    }

    @Override
    protected void showBack(TextView button_goback) {
        button_goback.setVisibility(View.VISIBLE);
    }

    @NonNull
    @Override
    protected int getLayoutId() {
        return R.layout.activity_opinion;
    }

    @Override
    protected void initEvent() {
        button_goback.setTypeface(iconfont);
        button_goback.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
        button_goback.setTextColor(getResources().getColor(R.color.main_color));
        button_goback.setText(getResources().getString(R.string.back_normal) + getResources().getString(R.string.back));
        content_et.addTextChangedListener(watcher);
    }

    TextWatcher watcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            tv_et_num.setText(content_et.getText().toString().trim().length() + "/240");
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    };

    @OnClick(R.id.tv_opinion)
    void opinion() {
        if (content_et.getText().toString().trim().isEmpty() || content_et.getText().toString().length() == 0) {
            Util.toast(this, getResources().getString(R.string.opinion_input));
            return;
        }
        if(content_et.getText().toString().trim().length()<10){
            ToastUtil.getToastEmail().ToastShow(this, null, 1, R.layout.toast_view, getResources().getString(R.string.opinion_content));
            return;
        }
        getPresenter().put(content_et.getText().toString().trim());
    }

    @Override
    public void showMsg(BaseJSON json) {
        if (json.returnCode.equals("0000")) {
//            Util.toast(this, "success");
            ToastUtil.getToastEmail().ToastShow(this, null, 1, R.layout.toast_view, getResources().getString(R.string.ssdk_sms_btn_submit)+getResources().getString(R.string.success_txt));
            content_et.setText("");
            finish();
        } else {
            Util.toast(this, json.returnMessage);
        }
    }
}
